package com.hw.netty;

import io.netty.bootstrap.ServerBootstrap;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.Channel;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelFutureListener;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import io.netty.handler.logging.LogLevel;
import io.netty.handler.logging.LoggingHandler;
import io.netty.util.CharsetUtil;
import io.netty.util.concurrent.Future;

import java.net.InetSocketAddress;
import java.util.Scanner;

public class NettyServer {
	
	private final int port;

	public NettyServer(int port) {
		this.port = port;
	}
	
	public static void main(String[] args) throws Exception {
//		if (args.length!=1) {
//			System.err.println(EchoServer.class.getSimpleName()+":请只输入一个端口号");
//			return;
//		}
//		
//		int port =Integer.parseInt(args[0]);
		
		System.out.println(NettyServer.class.getSimpleName());
		Scanner scanner=new Scanner(System.in);
		System.err.println("请输入端口号：");
		
		int port=scanner.nextInt();
		new NettyServer(port).start();
		
		
	}
	
	public void start() throws Exception {

		NioEventLoopGroup boss = new NioEventLoopGroup();
		NioEventLoopGroup worker = new NioEventLoopGroup();
		try {
			ServerBootstrap b=new ServerBootstrap();
			b.group(boss,worker)
			.channel(NioServerSocketChannel.class)
			.localAddress(new InetSocketAddress(port))
			.childHandler(new ChannelInitializer<SocketChannel>() {

				@Override
				protected void initChannel(SocketChannel ch) throws Exception {
					ch.pipeline().addLast(new LoggingHandler(LogLevel.DEBUG));
					ch.pipeline().addLast("Server Handler",new NettyServerHandler());
					
				}
			});
			ChannelFuture f =b.bind().sync();
			System.out.println(NettyServer.class.getName()+"started,"+f.channel().localAddress());
			
			/***********************************channel 相关操作******************************************************/
			
			f.addListener(new ChannelFutureListener() {
				
				@Override
				public void operationComplete(ChannelFuture future) throws Exception {
					if (future.isSuccess()) {
						System.out.println("channel success!");
						
						Channel channel=future.channel();
						ByteBuf buf=Unpooled.copiedBuffer("channel data!",CharsetUtil.UTF_8);
//						ChannelFuture future=channel.writeAndFlush(buf);
						channel.writeAndFlush(buf);
//						channel.pipeline();
						
					}else {
						System.out.println("channel error!");
						future.cause().printStackTrace();
					}
				}
			});
			/***********************************channel 相关操作******************************************************/
			
			
			f.channel().closeFuture().sync();
		}finally {
//			boss.shutdownGracefully().sync();
			Future<?> bossFuture=boss.shutdownGracefully();
			Future<?> workerFuture=worker.shutdownGracefully();
			bossFuture.sync();
			workerFuture.sync();
		}

		
		
	}

}
